<?php
/**
* 加载文件
* @param  string $classname 文件名
* @return  
*/
function Loader($classname) {
	$fileArr = explode('::',$classname);
	$pathType = strtoupper($fileArr[0]);
	if (count($fileArr) == 2) {
		if(in_array($pathType, array('APP','SYS'))){
			$path = $pathType == 'APP' ? APP_PATH : SYS_PATH;
			$loadFile = $path."/lib/{$fileArr[1]}.class.php";
		}elseif ($pathType == 'MOD') {
			$loadFile = APP_PATH."/models/{$fileArr[1]}.class.php";
		}
	}else{
		$loadFile = APP_PATH."/models/{$classname}.class.php";
	}

	if (file_exists($loadFile)) {
		require_once($loadFile);
	}else{
		if (SysConfig::$D_BUG) {
			exit("ERROR：File {$loadFile} is not exists");
		}
		
	}
	return '';
}

//获取IP
function getIp() { //获取IP
    if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    else if (isset($_SERVER["HTTP_CLIENT_IP"]))
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    else if ($_SERVER["REMOTE_ADDR"])
        $ip = $_SERVER["REMOTE_ADDR"];
    else if (getenv("HTTP_X_FORWARDED_FOR"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if (getenv("HTTP_CLIENT_IP"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if (getenv("REMOTE_ADDR"))
        $ip = getenv("REMOTE_ADDR");
    else
        $ip = "Unknown";
    return $ip;
}


//循环建立目录齐圣福2011年3月17日8:19:30修改
function mkdirs($path, $mode = 0777) {
    $dirs = explode('/', $path);
    $count = count($dirs);
    $pos = strrpos($dirs[$count - 1], '.');
    if ($pos) {
        $count = $count -1;
    }
    for ($c = 0; $c < $count; $c++) {
        $thispath = "";
        for ($cc = 0; $cc <= $c; $cc++) {
            $thispath .= $dirs[$cc] . '/';
        }
        if (!file_exists($thispath)) {
            @mkdir($thispath, $mode);
        }
    }
    return $pos ? file_exists(dirname($path)) : file_exists($path);
}

//字符串截取函数
function cutStr($string, $length, $charset = 'utf-8') {
	if(strlen($string) <= $length) {
		return $string;
	}
	$string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);
	$strcut = '';
	if(strtolower($charset) == 'utf-8') {

		$n = $tn = $noc = 0;
		while($n < strlen($string)) {
			$t = ord($string[$n]);
			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
				$tn = 1; $n++; $noc++;
			} elseif(194 <= $t && $t <= 223) {
				$tn = 2; $n += 2; $noc += 2;
			} elseif(224 <= $t && $t < 239) {
				$tn = 3; $n += 3; $noc += 2;
			} elseif(240 <= $t && $t <= 247) {
				$tn = 4; $n += 4; $noc += 2;
			} elseif(248 <= $t && $t <= 251) {
				$tn = 5; $n += 5; $noc += 2;
			} elseif($t == 252 || $t == 253) {
				$tn = 6; $n += 6; $noc += 2;
			} else {
				$n++;
			}
			if($noc >= $length) {
				break;
			}
		}
		if($noc > $length) {
			$n -= $tn;
		}
		$strcut = substr($string, 0, $n);
	} else {
		for($i = 0; $i < $length; $i++) {
			$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
		}
	}
	$strcut = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
	return $strcut.$dot;
}

/**
* 功能: 获取微妙级的时间戳
* 作者: php手册
* 最后一次修改时间: 2011年4月19日16:00:30
* 参数： 
*         
* 返回值:
*                
*/
function microtime_float()
{
	list($usec, $sec) = explode(" ", microtime());
	return ((float)$usec + (float)$sec);
}

//某个字符串是否存在某个字符
function strexists($haystack, $needle) {
	return !(strpos($haystack, $needle) === FALSE);
}

//判断是否是蜘蛛
function getrobot() {
	if(!defined('IS_ROBOT')) {
		$kw_spiders = 'Bot|Crawl|Spider|slurp|sohu-search|lycos|robozilla';
		$kw_browsers = 'MSIE|Netscape|Opera|Konqueror|Mozilla';
		if(!strexists($_SERVER['HTTP_USER_AGENT'], 'http://') && preg_match("/($kw_browsers)/i", $_SERVER['HTTP_USER_AGENT'])) {
			define('IS_ROBOT', FALSE);
		} elseif(preg_match("/($kw_spiders)/i", $_SERVER['HTTP_USER_AGENT'])) {
			define('IS_ROBOT', TRUE);
		} else {
			define('IS_ROBOT', FALSE);
		}
	}
	return IS_ROBOT;
}




